<?php
/*
 +----------------------------------------------------------------------
 + Title        : ArticleLogic 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/2/28 22:01
 + Last-time    : 2021/2/28 22:01+ chen2
 + Desc         : ArticleLogic
 +----------------------------------------------------------------------
*/

namespace app\common\service;


use app\common\model\Article;
use think\db\Query;
use think\facade\Db;

class ArticleService
{

    /**
     *
     * @param $params
     *
     * @return \think\Paginator
     * @throws \think\exception\DbException
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:39
     */
    public function getArticleList($params) : \think\Paginator
    {
        $query = Article::build()
                        ->with(
                            [
                                'articleCate' => function (Query $query)
                                {
                                    $query->where('is_deleted', 0);
                                }
                            ]
                        )
                        ->equal('article_cat_id,is_top,status,article_type')
                        ->keyWrod('title|description')
                        ->order('is_top desc , weight desc ,article_id desc');
        // 传入文章分类 , 则返回分类的文章
        if (isset($params['cate_type'])) {
            $query->whereIn(
                'article_cat_id', function (Query $query) use ($params)
            {
                $query->name('article_cate')
                      ->whereIn('cate_type', $params['cate_type'])
                      ->field('article_cat_id');
            }
            );
        }
        $list = $query->_paginate();
        return $list;
    }

    /**
     *
     * @param $id
     *
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:39
     */
    public function getArticleItem($id)
    {
        $item = Article::where('article_id', $id)
                       ->findOrFail();
        return $item;
    }

    /**
     *
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:39
     */
    public function addArticleItem($data)
    {
        Db::startTrans();
        try {
            $model = new Article();
            $model->save($data);//添加用户装修休息
            Db::commit();
            return $model;
        } catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
    }


    /**
     *
     * @param $params
     *
     * @return bool
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:40
     */
    public function setArticleItem($data)
    {
        Db::startTrans();
        try {
            $article = Article::build()
                              ->findOrFail($data['article_id']);
            $article->save($data);
            Db::commit();
            return $article;
        } catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
    }

    /**
     *
     * @author : Randy_chen
     * @Date   : 2021/3/2
     * @Time   : 22:40
     */
    public function delArticleItem(int $article_id) : bool
    {
        $article = Article::find($article_id);
        !$article && error_abort('文章不存在!');
        return $article->delete();
    }


    public function getArticleItemByType($cate_type)
    {
        $info = Article::build()
                       ->whereIn(
                           'article_cat_id', function (Query $query) use ($cate_type)
                       {
                           $query->name('article_cate')
                                 ->where('cate_type', $cate_type)
                                 ->field('article_cat_id');
                       }
                       )
                       ->order('is_top desc, weight desc')
                       ->find();

        return $info;
    }

}